package com.victor.controller.partner_controller;

import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.victor.entity.Log;
import com.victor.entity.hotel.Hotel;
import com.victor.entity.hotel.HotelPhoto;
import com.victor.entity.hotel.HotelPhotoGroup;
import com.victor.entity.hotel.HotelRoom;
import com.victor.entity.hotel.HotelRoomExplain;
import com.victor.entity.hotel.HotelRoomLabel;
import com.victor.entity.hotel.HotelRoomPhoto;
import com.victor.entity.hotel.HotelType;
import com.victor.service.PartnersService;
import com.victor.utils.DateUtils;
import com.victor.utils.Msg;

/**
 * 宾馆
 * 
 * @author zhangbin
 *
 */
@RestController
@RequestMapping("/partner/hotel")
public class Hotel_C {

	@Autowired
	private PartnersService service;

	// 获取宾馆列表--通过分类id和搜索内容
	@RequestMapping("/getHotelList")
	public Msg getHotelList(String t_id, String search) {
		List<Hotel> hotels = service.getHotelList(t_id, search);
		return Msg.success().add("hotels", hotels);
	}

	// 查询宾馆详情
	@RequestMapping("/getHotelXQ")
	public Msg getHotelXQ(String h_id) {
		Hotel hotel = service.getHotelXQ(h_id);
		if (hotel != null) {

			return Msg.success().add("hotel", hotel);
		}
		return Msg.fail("未查询到相关信息");
	}

	// 删除评论
	@RequestMapping("/delComment")
	public Msg delComment(String c_id, String adminid) {
		boolean delResult = service.delHotelComment(c_id);
		if (delResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("删除了宾馆的评论");

			return Msg.success();
		}
		return Msg.fail("未查询到相关信息");
	}

	// 批量导出
	@RequestMapping("batchExport")
	public void batchExport(String adminid, String t_id, String search, HttpServletResponse response,
			HttpServletRequest request) throws Exception {
		// 获取宾馆列表--通过分类id和搜索内容
		List<Hotel> hotels = service.getHotelList(t_id, search);

		// 创建poi导出数据对象
		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
		// 创建sheet页
		Sheet sheet = sxssfWorkbook.createSheet("宾馆商家列表");

		CellRangeAddress region1 = new CellRangeAddress(0, 1, (short) 0, (short) 6);
		// 参数1：起始行 参数2：终止行 参数3：起始列 参数4：终止列
		sheet.addMergedRegion(region1);
		Row headTitle = sheet.createRow(0);
		headTitle.createCell(0).setCellValue("宾馆商家列表");

		// 创建表头
		Row headRow = sheet.createRow(4);
		// 设置表头信息
		headRow.createCell(0).setCellValue("名称");
		headRow.createCell(1).setCellValue("星级");
		headRow.createCell(2).setCellValue("价格");
		headRow.createCell(3).setCellValue("位置--全");
		headRow.createCell(4).setCellValue("位置--省");
		headRow.createCell(5).setCellValue("位置--市");
		headRow.createCell(6).setCellValue("位置--区");
		headRow.createCell(7).setCellValue("位置--街道");
		headRow.createCell(8).setCellValue("位置--详细");
		headRow.createCell(9).setCellValue("位置备注");
		headRow.createCell(10).setCellValue("酒店介绍");
		headRow.createCell(11).setCellValue("酒店优惠");
		headRow.createCell(12).setCellValue("酒店介绍视频");
		headRow.createCell(13).setCellValue("是否为首页推荐 0否 1是");
		headRow.createCell(14).setCellValue("宾馆类型表id");
		headRow.createCell(15).setCellValue("创建人id");
		headRow.createCell(16).setCellValue("是否冻结标志 0.正常 1.冻结");
		headRow.createCell(17).setCellValue("冻结起始时间");
		headRow.createCell(18).setCellValue("冻结终止时间");
		headRow.createCell(19).setCellValue("冻结周期 0.终身冻结 1.一小时, 2.一天, 3.一周, 4.一个月,5.一年,6.二年,7.三年,");
		headRow.createCell(20).setCellValue("冻结原由标题");
		headRow.createCell(21).setCellValue("冻结原由详情");

		// 遍历上面数据库查到的数据
		for (Hotel hotel : hotels) {
			Row dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
			dataRow.createCell(0).setCellValue(hotel.getH_name());
			dataRow.createCell(1).setCellValue(hotel.getH_star());
			dataRow.createCell(2).setCellValue(hotel.getH_price());
			dataRow.createCell(3).setCellValue(hotel.getH_position());
			dataRow.createCell(4).setCellValue(hotel.getH_position_province());
			dataRow.createCell(5).setCellValue(hotel.getH_position_city());
			dataRow.createCell(6).setCellValue(hotel.getH_position_area());
			dataRow.createCell(7).setCellValue(hotel.getH_position_street());
			dataRow.createCell(8).setCellValue(hotel.getH_position_detailed());
			dataRow.createCell(9).setCellValue(hotel.getH_position_remarks());
			dataRow.createCell(10).setCellValue(hotel.getH_introduce());
			dataRow.createCell(11).setCellValue(hotel.getH_discount());
			dataRow.createCell(12).setCellValue(hotel.getH_video());
			dataRow.createCell(13).setCellValue(hotel.getH_ifhomepage());
			dataRow.createCell(14).setCellValue(hotel.getT_id());
			dataRow.createCell(15).setCellValue(hotel.getAdminid());
			dataRow.createCell(16).setCellValue(hotel.getH_frozen());
			dataRow.createCell(17).setCellValue(hotel.getH_frozen_starttime());
			dataRow.createCell(18).setCellValue(hotel.getH_frozen_endtime());
			dataRow.createCell(19).setCellValue(hotel.getH_frozen_cycle());
			dataRow.createCell(20).setCellValue(hotel.getH_frozen_title());
			dataRow.createCell(21).setCellValue(hotel.getH_frozen_details());

		}
		// 下载导出文件名
		String filename = "宾馆商家列表";
		// 设置头信息
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");
		// 一定要设置成xlsx格式
		response.setHeader("Content-Disposition",
				"attachment;filename=" + URLEncoder.encode(filename + ".xlsx", "UTF-8"));
		// 创建一个输出流
		ServletOutputStream outputStream = response.getOutputStream();
		// 写入数据
		sxssfWorkbook.write(outputStream);
		// 关闭
		outputStream.close();
		sxssfWorkbook.close();

		// 添加操作日志
		Log log = new Log();
		log.setDate(DateUtils.getNowDate());
		log.setTime(DateUtils.getNowTime());
		log.setAdminid(adminid);
		log.setContent("批量导出宾馆商家列表");
	}

	// 添加宾馆
	@RequestMapping("/addHotel")
	public Msg addHotel(Hotel hotel, String adminid) {
		boolean insResult = service.addHotel(hotel);
		if (insResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("添加宾馆");

			return Msg.success();
		}
		return Msg.fail();
	}

	// 删除宾馆
	@RequestMapping("/delHotel")
	public Msg delHotel(Integer h_id, String adminid) {
		boolean delResult = service.delHotel(h_id);
		if (delResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("删除宾馆");

			return Msg.success();
		}
		return Msg.fail();
	}

	// 获取所有类型
	@RequestMapping("/getHotelType")
	public Msg getHotelType() {
		List<HotelType> hotelTypes = service.getHotelType();
		return Msg.success().add("hotelTypes", hotelTypes);
	}

	// 修改宾馆信息
	@RequestMapping("/updHotel")
	public Msg updHotel(Hotel hotel, String adminid) {
		boolean updResult = service.updHotel(hotel);
		if (updResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("修改宾馆信息");
			return Msg.success();
		}
		return Msg.fail();

	}

	// 设置-取消 首页推荐 updHotel方法传id和推荐字段

	// 添加图片组
	@RequestMapping("/addHotelPhotoGroup")
	public Msg addHotelPhotoGroup(HotelPhotoGroup hotelPhotoGroup, String adminid) {

		boolean insResult = service.addHotelPhotoGroup(hotelPhotoGroup);
		if (insResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("宾馆添加图片组");
			return Msg.success();
		}
		return Msg.fail();
	}

	// 组内添加图片
	@RequestMapping("/addHotelPhoto")
	public Msg addHotelPhoto(HotelPhoto hotelPhoto, String adminid) {

		boolean insResult = service.addHotelPhoto(hotelPhoto);
		if (insResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("宾馆添加图片");
			return Msg.success();
		}
		return Msg.fail();
	}

	// 删除组
	@RequestMapping("/delHotelPhotoGroup")
	public Msg delHotelPhotoGroup(Integer g_id, String adminid) {
		boolean delResult = service.delHotelPhotoGroup(g_id);
		if (delResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("宾馆删除组");
			return Msg.success();
		}
		return Msg.fail();
	}

	// 删除图片
	@RequestMapping("/delHotelPhoto")
	public Msg delHotelPhoto(String p_id, String adminid) {
		boolean delResult = service.delHotelPhoto(p_id);
		if (delResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("宾馆删除图片");
			return Msg.success();
		}
		return Msg.fail();
	}

	// 获取宾馆房间列表(传宾馆id)
	@RequestMapping("/getHotelRoomByHotelid")
	public Msg getHotelRoomByHotelid(String h_id) {
		List<HotelRoom> hotelRooms = service.getHotelRoomByHotelid(h_id);

		return Msg.success().add("hotelRooms", hotelRooms);
	}

	// 获取宾馆房间详情
	@RequestMapping("/getHotelRoomXQ")
	public Msg getHotelRoomXQ(String r_id) {
		HotelRoom hotelRoom = service.getHotelRoomXQ(r_id);
		if(hotelRoom!=null) {
			
			return Msg.success().add("hotelRoom", hotelRoom);
		}
		return Msg.fail();
	}

	// 删除房间
	@RequestMapping("/delRoom")
	public Msg delRoom(Integer r_id, String adminid) {

		boolean delResult = service.delRoom(r_id);
		if (delResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("删除房间");
			return Msg.success();
		}
		return Msg.fail();
	}

	// 添加房间
	@RequestMapping("/addRoom")
	public Msg addRoom(@RequestBody HotelRoom hotelRoom, @RequestParam String adminid) {
		// 添加房间信息返回id
		boolean insResult = service.addRoom(hotelRoom);
		if (insResult) {
			// 添加自定义标签list
			if (hotelRoom.getHotelRoomLabels() != null && hotelRoom.getHotelRoomLabels().size() > 0) {
				for (HotelRoomLabel hotelRoomLabel : hotelRoom.getHotelRoomLabels()) {
					hotelRoomLabel.setR_id(String.valueOf(hotelRoom.getR_id()));
					service.addHotelRoomLabel(hotelRoomLabel);
				}
			}
			// 添加预定说明list
			if (hotelRoom.getHotelRoomExplains() != null && hotelRoom.getHotelRoomExplains().size() > 0) {
				for (HotelRoomExplain hotelRoomExplain : hotelRoom.getHotelRoomExplains()) {
					hotelRoomExplain.setR_id(String.valueOf(hotelRoom.getR_id()));
					service.addHotelRoomExplain(hotelRoomExplain);
				}
			}
			// 添加图片list
			if (hotelRoom.getHotelRoomPhotos() != null && hotelRoom.getHotelRoomPhotos().size() > 0) {
				for (HotelRoomPhoto hotelRoomPhoto : hotelRoom.getHotelRoomPhotos()) {
					hotelRoomPhoto.setR_id(String.valueOf(hotelRoom.getR_id()));
					service.addHotelRoomPhoto(hotelRoomPhoto);
				}
			}
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("添加了宾馆的房间");

			return Msg.success();
		}

		return Msg.fail("添加房间信息失败");
	}

	// 修改房间信息
	@RequestMapping("/updRoom")
	public Msg updRoom(HotelRoom hotelRoom, String adminid) {
		boolean updResult = service.updRoom(hotelRoom);

		if (updResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("修改宾馆房间信息");
			return Msg.success();
		}
		return Msg.fail();
	}

	// 添加房间图片
	@RequestMapping("/addRoomPhoto")
	public Msg addRoomPhoto(HotelRoomPhoto hotelRoomPhoto, String adminid) {

		boolean insResult = service.addRoomPhoto(hotelRoomPhoto);
		if (insResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("添加宾馆房间图片");
			return Msg.success();
		}
		return Msg.fail();

	}

	// 删除房间图片
	@RequestMapping("/delRoomPhoto")
	public Msg delRoomPhoto(String p_id, String adminid) {

		boolean delResult = service.delRoomPhoto(p_id);
		if (delResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("删除宾馆房间图片");
			return Msg.success();
		}
		return Msg.fail();

	}

	// 冻结
	// frozen_title;// 冻结原由标题
	// frozen_details;// 冻结原由详情
	// frozen_starttime;//冻结开始时间
	// frozen_endtime;//冻结结束时间
	// frozen_cycle;// 冻结周期 0.终身冻结 1.1小时, 2.1天, 3.一周, 4.一个月,5.一年,6.二年,7.三年,
	@RequestMapping("/frozenHotel")
	public Msg frozenHotel(Integer h_id, String adminid, String frozen_cycle, String frozen_starttime,
			String frozen_endtime, String frozen_title, String frozen_details) {

		boolean updResult = service.frozenHotel(h_id, frozen_cycle, frozen_starttime, frozen_endtime, frozen_title,
				frozen_details);
		if (updResult) {

			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("冻结了id为" + h_id + "的宾馆");
			return Msg.success();
		}
		return Msg.fail();
	}

}
